<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2017/12/23
 * Time: 下午4:58
 */

namespace Biz\module\Classes\adm;

use Biz\model\ShopBasicInfo as mShopBasicInfo;
use Biz\model\ShopCompanyInfo as mShopCompanyInfo;
use Biz\model\ShopPersonalInfo as mShopPersonalInfo;
use Biz\model\ShopSecurityDeposit as mShopSecurityDeposit;
use Biz\module\Classes\core\SearchListAbstract;
use Doraemon\model\Member as mMember;
use Doraemon\model\Phone as mPhone;
use Medoo\Medoo;


final class ShopSearch extends SearchListAbstract
{

    protected $sbiWhere = [];
    protected $sciWhere = [];
    protected $ssdWhere = [];
    protected $count = 0;

    protected function buildSelect()
    {
        $this->select = [
            'sbi.uid',
        ];
    }

    protected function buildWhere()
    {

        //状态
        if ($this->params['status'] > -1) {
            $this->sbiWhere['sbi.status'] = $this->params['status'];
        }
        //类型
        if ($this->params['type'] > -1) {
            $this->sbiWhere['sbi.type'] = $this->params['type'];
        }

        //名称
        if ($this->params['name']) {
            if ($this->params['type'] > -1) {
                if ($this->params['type'] == mShopBasicInfo::TYPE_COMPANY) {
                    $this->sciWhere['name[~]'] = $this->params['name'];
                } else {
                    $this->sbiWhere['sbi.type'] = 0;
                }
            } else {
                $this->sciWhere['name[~]'] = $this->params['name'];
            }
        }

        if ($this->params['start_date']) {
            $this->sbiWhere['sbi.addtime[>]'] = $this->params['start_date'];
        }


        if ($this->params['stop_date']) {
            $this->sbiWhere['sbi.addtime[<]'] = date('Y-m-d', strtotime($this->params['stop_date']) + 86400);
        }

        //保证金状态
        if ($this->params['deposit_status'] > -1) {
            if ($this->params['deposit_status'] != mShopSecurityDeposit::STATUS_DEBT) {
                $this->ssdWhere['status'] = $this->params['deposit_status'];
            } else {
                $this->ssdWhere['amount[>]'] = Medoo::raw('`balance`');
            }
        }

        //自定义搜索条件
        if (!empty($this->params['field'])) {
            if (!empty($this->params['keyword'])) {
                if (in_array($this->params['field'], ['uid'])) {
                    $this->sbiWhere['sbi.' . $this->params['field']] = $this->params['keyword'];
                }
                if (in_array($this->params['field'], ['name'])) {
                    $this->sbiWhere['sbi.' . $this->params['field'] . '[~]'] = $this->params['keyword'];
                }
            }
        }


        return true;


        //状态
        if ($this->params['status'] > -1) {
            $this->where['sbi.status'] = $this->params['status'];
        }
        //类型
        if ($this->params['type'] > -1) {
            $this->where['sbi.type'] = $this->params['type'];
        }

        //名称
        if ($this->params['name']) {
            if ($this->params['type'] > -1) {
                if ($this->params['type'] == mShopBasicInfo::TYPE_COMPANY) {
                    $this->where['sci.name[~]'] = $this->params['name'];
                } else {
                    $this->where['sbi.type'] = 0;
                }
            } else {
                $this->where['sci.name[~]'] = $this->params['name'];
            }
        }

        if ($this->params['start_date']) {
            $this->where['sbi.addtime[>]'] = $this->params['start_date'];
        }


        if ($this->params['stop_date']) {
            $this->where['sbi.addtime[<]'] = date('Y-m-d', strtotime($this->params['stop_date']) + 86400);
        }

        //保证金状态
        if ($this->params['deposit_status'] > -1) {
            if ($this->params['deposit_status'] != mShopSecurityDeposit::STATUS_DEBT) {
                $this->where['ssd.status'] = $this->params['deposit_status'];
            } else {
                $this->where['ssd.amount[>]'] = Medoo::raw('`ssd`.balance');
            }
        }

        //自定义搜索条件
        if (!empty($this->params['field'])) {
            if (!empty($this->params['keyword'])) {
                if (in_array($this->params['field'], ['uid'])) {
                    $this->where['sbi.' . $this->params['field']] = $this->params['keyword'];
                }
                if (in_array($this->params['field'], ['name'])) {
                    $this->where['sbi.' . $this->params['field'] . '[~]'] = $this->params['keyword'];
                }
            }
        }


    }

    public function getCount()
    {
        return $this->count;
    }

    public function getList()
    {
        $uids = mShopBasicInfo::select('uid', array_merge($this->sbiWhere, ['ORDER' => ['sbi.addtime' => 'DESC']]));
        if ($uids && $this->sciWhere) {
            $this->sciWhere['shop_id'] = $uids;
            $uids = mShopCompanyInfo::select('shop_id', $this->sciWhere);
        }
        if ($uids && $this->ssdWhere) {
            $this->ssdWhere['shop_id'] = $uids;
            $uids = mShopSecurityDeposit::select('shop_id', $this->ssdWhere);
        }
        $this->count = count($uids);
        $uids = array_slice($uids, $this->limit[0] , $this->limit[1]);
        if ($uids) {
            $infoList = mShopBasicInfo::getInfoList($uids, 'adm');

            array_walk($infoList, function ($v) use (&$data) {
                $data[$v['type']][] = $v['shop_id'];
            });

            if (isset($data[mShopBasicInfo::TYPE_PERSONAL])) {
                $personList = mShopPersonalInfo::select('*', ['shop_id' => $data[mShopBasicInfo::TYPE_PERSONAL]]);

                $personNames = array_column($personList, 'realname', 'shop_id');
            }

            if (isset($data[mShopBasicInfo::TYPE_COMPANY])) {
                $companyList = mShopCompanyInfo::select('*', ['shop_id' => $data[mShopBasicInfo::TYPE_COMPANY]]);

                $companyNames = array_column($companyList, 'name', 'shop_id');
            }

            $memberList = mMember::getUserList($uids, true);

            $phoneList = mPhone::getPhoneList($uids);

            $depositList = mShopSecurityDeposit::getDepositList($uids);

            if ($infoList) {
                foreach ($infoList as &$info) {

                    if ($info['type'] == mShopBasicInfo::TYPE_PERSONAL) {
                        $info['company_name'] = isset($personNames[$info['shop_id']]) ? $personNames[$info['shop_id']] : '';
                    } else {
                        $info['company_name'] = isset($companyNames[$info['shop_id']]) ? $companyNames[$info['shop_id']] : '';
                    }

                    $info['member'] = isset($memberList[$info['uid']]) ? $memberList[$info['uid']] : [];

                    $info['phone'] = isset($phoneList[$info['uid']]) ? $phoneList[$info['uid']]['hidden_phone'] : '';

                    $info['deposit'] = isset($depositList[$info['uid']]) ? $depositList[$info['uid']] : [];
                }
            }


            return $infoList;
        }
    }

}
